package com.yndsoft.jc.supplier.adapter;

import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.yndsoft.jc.R;
import com.yndsoft.jc.supplier.net.ApiService;
import com.yndsoft.jc.supplier.order.OnClickPositionListener;
import com.yndsoft.jc.supplier.order.OnDeletePositionListener;
import com.yndsoft.jc.supplier.order.PreviewPictureActivity;
import com.yndsoft.jc.supplier.utils.Constants;
import com.yndsoft.jc.supplier.widget.imageselector.ImageLoader;
import com.yndsoft.jc.supplier.widget.imageselector.ImgSelActivity;
import com.yndsoft.jc.supplier.widget.imageselector.ImgSelConfig;
import com.yndsoft.jc.supplier.widget.recyclerview.RecyclerAdapter;
import com.yndsoft.jc.supplier.widget.recyclerview.RecyclerHolder;
import com.yndsoft.jc.supplier.widget.recyclerview.RecyclerListener;

import java.util.ArrayList;
import java.util.List;

public class OrderDetailImgAdapter extends RecyclerAdapter<String> implements RecyclerListener.OnClickListener<RecyclerHolder> {

    private Activity activity;
    private String tag;
    private int parentPosition;
    private String oDetId;
    private ImageLoader loader = new ImageLoader() {
        @Override
        public void displayImage(Context context, String path, ImageView imageView) {
            Glide.with(context)
                    .load(path)
                    .placeholder(R.drawable.ic_default_image)
                    .centerCrop()
                    .into(imageView);
        }
    };
    private OnClickPositionListener onClickPositionListener;
    private OnDeletePositionListener onDeletePositionListener;

    public void setOnClickPositionListener(OnClickPositionListener onClickPositionListener) {
        this.onClickPositionListener = onClickPositionListener;
    }

    public void setOnDeletePositionListener(OnDeletePositionListener onDeletePositionListener) {
        this.onDeletePositionListener = onDeletePositionListener;
    }

    public OrderDetailImgAdapter(Context context, Activity activity, List<String> data, String tag, int parentPosition, String oDetId) {
        super(context, data, R.layout.item_order_detail_img);
        this.activity = activity;
        this.tag = tag;
        this.parentPosition = parentPosition;
        this.oDetId = oDetId;
    }

    @Override
    public void convert(RecyclerHolder holder, String model) {
        ImageView img = holder.findViewById(R.id.img);
        ImageView del = holder.findViewById(R.id.del);
        if (tag != null && tag.equals(Constants.ENTRY)) {
            del.setVisibility(View.VISIBLE);
            if (model.equals(Constants.PLACE_HOLDER)) {
                Glide.with(mContext)
                        .load(R.mipmap.imgadd)
                        .centerCrop()
                        .into(img);
                del.setVisibility(View.GONE);
            } else {
                Glide.with(mContext).load(model).into(img);
            }
        } else {
            Glide.with(mContext).load(ApiService.BASE_URL + model).into(img);
        }
        holder.setOnClickListener(this, R.id.img, R.id.del);
    }

    @Override
    public void onClick(View v, int position, RecyclerHolder holder) {
        switch (v.getId()) {
            case R.id.img:
                if (onClickPositionListener != null) {
                    onClickPositionListener.onClickPosition(parentPosition, oDetId);
                }
                if (mData.size() - 1 == position) {
                    ImgSelConfig config = new ImgSelConfig.Builder(mContext, loader)
                            .multiSelect(false)
                            .rememberSelected(false)
                            .needCamera(true)
                            .maxNum(1)
                            .build();
                    ImgSelActivity.startActivity(activity, config, Constants.REQUEST_ENTRY_CODE);
                } else {
                    PreviewPictureActivity.start(mContext, position, (ArrayList<String>) mData);
                }
                break;
            case R.id.del:
                if (onDeletePositionListener != null) {
                    onDeletePositionListener.onDeletePosition(parentPosition, position);
                    remove(position);
                }
                break;
        }
    }
}
